<?php

/**
 * @version  1.0
 * @Project  Article Link Summary
 * @author   Lars Echterhoff
 * @package  
 * @copyright Copyright (C) 2012 Echterhoff Medientechnik
 * @license  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgContentarticlelinksummary extends JPlugin
{

	function plgContentarticlelinksummary(&$subject, $params)
	{
		parent::__construct($subject, $params);
	}

	function onContentBeforeDisplay($context, &$article, &$params, $page=0)
	{
		$ignore_pagination = $this->params->get('ignore_pagination');
		$view = JRequest::getCmd('view');
		if( $view == 'article' )
		{
			$this->InjectCode($article, $params, 1, $view);
		}
	}

	function onBeforeCompileHead()
	{
		$this->InjectHeadCode();
	}

	private function InjectHeadCode()
	{
		//Currently no use and just for remind purposes still in here
		return;
	}

	private function InjectCode(&$article, &$params, $modus, $view)
	{
		$document = & JFactory::getDocument();
		$position = $this->params->get('position', '');
		$container_links = $this->params->get('container_links', 'div');
		$header_text = $this->params->get('header_text', '');
		$footer_text = $this->params->get('footer_text', '');
		$container_class = $this->params->get('container_class', '');

		$exclude_link_content = $this->params->get('exclude_link_content', 'dropimages'); //never, dropimages*, dropimagelinks, striphtml
		$treat_unnamed_links = $this->params->get('treat_unnamed_links', 'hrefascaption'); //hrefascaption*, dropunnamed, alwayshrefascaption

		$title = htmlentities($article->title, ENT_QUOTES, "UTF-8");
		$url = $this->getPageUrl($article);
		$basetitle = $document->getTitle();

		if( $view == 'category' )
		{
			$baseurl = $this->getCatUrl($article);
		}
		else
		{
			$baseurl = $document->getBase();
		}


		$category_tobe_excluded = $this->params->get('category_tobe_excluded', '');
		$content_tobe_excluded = $this->params->get('content_tobe_excluded', '');
		$excludedContentList = @explode(",", $content_tobe_excluded);


		if( $article->id != null )
		{
			if( in_array($article->id, $excludedContentList) )
			{
				return;
			}
			if( is_array($category_tobe_excluded) && in_array($article->catid, $category_tobe_excluded) )
			{
				return;
			}
		}
		else
		{
			if( is_array($category_tobe_excluded) && in_array(JRequest::getCmd('id'), $category_tobe_excluded) ) return;
		}

		if( $view != 'article' )
		{
			$tmp = $article->introtext;
		}
		else
		{
			$tmp = $article->text;
		}

		$matches = preg_match_all('#(<[ ]*a [ \t]*(?:[^>"\']|"(?:[^"]*")|\'(?:[^\']*\'))*>)(.*?)(<[ ]*/[ ]*a[ ]*>)#is', $tmp, $match, PREG_SET_ORDER);

		$htmlcode = '<div'.($container_class ? ' class="'.$container_class.'"' : '').'>';

		if( $container_links ) $htmlcode.= '<'.$container_links.' class="articlelinksummary">';
		if( $header_text ) $htmlcode.= '<div class="header">'.$header_text.'</div>';

		$htmlcode.= '<ul id="articlelinksummary">';

		if( !$matches ) return true;
		foreach( $match as $id => $link )
		{

			preg_match('/(?:href[ \t]*=[ \t]*(?:"|\'|)([^"\' ]*)(?:\'|")?)/is', $link[1], $hrefmatch);
			$href = $hrefmatch[1];

			switch ($exclude_link_content)
			{
				case "dropimages":
					$link[2] = preg_replace("#<[ ]*img [^>]*?>#is", "", $link[2]);
					$link[2] = trim($link[2]);
					if( $link[2] == "" ) $link[2] = "";
					break;
				case "dropimagelinks":
					$link[2] = preg_replace("#<[ ]*img [^>]*?>#is", "", $link[2]);
					$link[2] = trim($link[2]);
					if( $link[2] == "" ) $link[2] = false;
					break;
				case "striphtml":
					$link[2] = strip_tags($link[2]);
					break;
				case "never":
					//As it is!
					break;
			}

			switch ($treat_unnamed_links)
			{
				case "hrefascaption":
					if( $link[2] === "" ) $link[2] = $href;
					break;
				case "alwayshrefascaption":
					$link[2] = $href;
					break;
				case "dropunnamed":
					if( $link[2] === "" ) $link[2] = false;
					break;
			}

			if( strpos($link[2], "http://") === 0 ) $link[2] = substr($link[2], strlen("http://"));


			if( $link[2] )
			{
				$htmlcode.= "<li>";
				$htmlcode.= $link[1];

				$htmlcode.= $link[2];
				$htmlcode.= $link[3];
				$htmlcode.= "</li>";
			}
		}

		$htmlcode.= "</ul>";

		if( $footer_text ) $htmlcode.= '<div class="header">'.$footer_text.'</div>';
		if( $container_links ) $htmlcode.= "</".$container_links.">";

		$htmlcode.= "</div>";

		if( $position == '1' )
		{
			$tmp = $htmlcode.$tmp;
		}
		if( $position == '2' )
		{
			$tmp = $tmp.$htmlcode;
		}
		if( $position == '3' )
		{
			$tmp = $htmlcode.$tmp.$htmlcode;
		}

		if( $view != 'article' )
		{
			$article->introtext = $tmp;
		}
		else
		{
			$article->text = $tmp;
		}
	}

	private function getCatUrl($obj)
	{
		if( !is_null($obj) && (!empty($obj->catid)) )
		{
			$url = JRoute::_(ContentHelperRoute::getCategoryRoute($obj->catid));
			$uri = JURI::getInstance();
			$base = $uri->toString(array('scheme', 'host', 'port'));
			$url = $base.$url;
			$url = JRoute::_($url, true, 0);
			return $url;
		}
	}

	private function getPageUrl($obj)
	{
		if( !is_null($obj) && (!empty($obj->catid)) )
		{
			if( empty($obj->catslug) )
			{
				$url = JRoute::_(ContentHelperRoute::getArticleRoute($obj->slug, $obj->catid));
			}
			else
			{
				$url = JRoute::_(ContentHelperRoute::getArticleRoute($obj->slug, $obj->catslug));
			}
			$uri = JURI::getInstance();
			$base = $uri->toString(array('scheme', 'host', 'port'));
			$url = $base.$url;
			$url = JRoute::_($url, true, 0);
			return $url;
		}
	}

}
?>

